package com.xianxing.gateway.log.interceptor;

import com.xianxing.gateway.log.service.RequestLogService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 拦截请求, 添加请求日志
 * @author 唐保罗
 * @version 1.0
 */
public class RequestLogInterceptor implements RequestInterceptor {

    @Autowired
    private RequestLogService requestLogService;

    @Override
    public void apply(RequestTemplate template) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null){
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            if (request != null){
                requestLogService.addAccessLog(request);
            }
        }
    }
}
